﻿using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{

    public class ForeignCustomers : Entity
    {
        ///Default constructor
        public ForeignCustomers()
        {

        }

        public override string TableName()
        {
            return "Customers";
        }

        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - Customercustomerdemo ( Customercustomerdemo_fk_customercustomerdemo_customers )
        ///    - Orders ( Orders_fk_orders_customers )
        /// </summary>
        public ForeignCustomers(params Entity[] entities)
            : this(true, entities)
        {
        }
        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - Customercustomerdemo ( Customercustomerdemo_fk_customercustomerdemo_customers )
        ///    - Orders ( Orders_fk_orders_customers )
        /// </summary>
        public ForeignCustomers(bool isFlat, params Entity[] entities)
            : this()
        {
            foreach (Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, new EntityCollection(entity), null);
                }
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(isFlat); 
        }

        public enum Fields
        {
            Customerid,
            CompanyName,
            ContactName,
            ContactTitle,
            Address,
            City,
            Region,
            Postalcode,
            Country,
            Phone,
            Fax
        } ;

        [IsNotNullable]
        [IsPK]
        [MaxLength(5)]
        public String Customerid { get; set; }

        [IsNotNullable]
        [MaxLength(40)]
        public String Companyname { get; set; }

        [MaxLength(30)]
        public String Contactname { get; set; }

        [MaxLength(30)]
        public String Contacttitle { get; set; }

        [MaxLength(60)]
        public String Address { get; set; }

        [MaxLength(15)]
        public String City { get; set; }

        [MaxLength(15)]
        public String Region { get; set; }

        [MaxLength(10)]
        public String Postalcode { get; set; }

        [MaxLength(15)]
        public String Country { get; set; }

        [MaxLength(24)]
        public String Phone { get; set; }

        [MaxLength(24)]
        public String Fax { get; set; }

        #region Customercustomerdemo
        private EntityCollection _Customercustomerdemo;
        [NotInTable]
        [TypeContained(typeof(Customercustomerdemo), "Customercustomerdemo")]
        public EntityCollection Customercustomerdemo
        {
            get
            {
                return _Customercustomerdemo;
            }

            set
            {
                _Customercustomerdemo = value;
            }
        }
        private Customercustomerdemo _Customercustomerdemo_;
        [NotInTable]
        public Customercustomerdemo Customercustomerdemo_
        {
            get
            {
                return _Customercustomerdemo_;
            }

            set
            {
                _Customercustomerdemo_ = value;
            }
        }
        #endregion

        #region Orders
        private EntityCollection _Orders;
        [NotInTable]
        [TypeContained(typeof(Orders), "ForeignOrders")]
        public EntityCollection Orders
        {
            get
            {
                return _Orders;
            }

            set
            {
                _Orders = value;
            }
        }
        private Orders _Orders_;
        [NotInTable]
        public Orders Orders_
        {
            get
            {
                return _Orders_;
            }

            set
            {
                _Orders_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "Customercustomerdemo", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Customerid");
                right.Add(GetType().Name + ".Customerid");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "ForeignOrders", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Customerid");
                right.Add(GetType().Name + ".Customerid");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            return null;
        }

    }
}


